/*
 * Copyright 2020-2030, MateCloud, DAOTIANDI Technology Inc All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Author: pangu(7333791@qq.com)
 */
package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.service.MemberAccountService;
import com.yc.cloud.admin.vo.MemberAccountVo;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.entity.MemberAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author Yancy
 * @since 2023-12-30
 */
@RestController
@RequestMapping("/member/accounts")
@Api(value = "会员", tags = "会员接口")
public class MemberAccountController {

    @Resource
    private MemberAccountService memberAccountService;


    @GetMapping
    @ApiOperation(value = "会员列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<MemberAccountVo>> list(BasePageReqDTO param) {
        val result = memberAccountService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "会员详情", notes = "根据ID查询")
    public ResponseResult<MemberAccount> getItem(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        return ResponseResult.success(memberAccountService.getById(id));
    }


    @PostMapping
    @ApiOperation(value = "添加会员", notes = "添加")
    public ResponseResult<MemberAccount> create(@Validated @RequestBody MemberAccount memberAccount) {
        val result = memberAccountService.save(memberAccount);
        if (result) {
            return ResponseResult.success(memberAccount);
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "删除会员", notes = "通过ID删除")
    @DeleteMapping(value = "{id}")
    public ResponseResult<Long> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = memberAccountService.removeById(id);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }


    @ApiOperation(value = "修改会员", notes = "通过ID修改")
    @PutMapping(value = "{id}")
    public ResponseResult<Long> update(@PathVariable @ApiParam(value = "ID", required = true) Long id, @Validated @RequestBody MemberAccount memberAccount) {
        memberAccount.setId(id);
        val result = memberAccountService.updateById(memberAccount);
        if (result) {
            return ResponseResult.success();
        }
        return ResponseResult.failed();
    }
}
